package com.cqjtu.graduation.service.impl;

import com.cqjtu.graduation.dao.UserDAO;
import com.cqjtu.graduation.entity.UserDO;
import com.cqjtu.graduation.util.FileServerCommon;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class UserDOServiceImpl implements com.cqjtu.graduation.service.UserDOService {
    @Autowired
    private UserDAO userDAO;
    @Override
    public UserDO login(UserDO userDO) {
        return userDAO.getUserDOByCondition(userDO);
    }

    @Override
    public void register(UserDO userDO , MultipartFile file) {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String format = df.format(new Date());
        format = format + new Random().nextLong();
        String url = FileServerCommon.FILE_SERVER_HOST + format + ".jpg";//要将图片保存到网络上的完整地址
        String partUrl = format + ".jpg";// 部分地址  /xxx.jpg
        //创建Jersey框架的客户端
        Client client = new Client();
        WebResource resource = client.resource(url);
        try {
            byte[] buf;
            buf = file.getBytes();
            resource.put(String.class,buf);//通过put方式向网络url发送资源
        } catch (IOException e) {
            e.printStackTrace();
        }
        userDO.setUserPhotoPath(url);
        userDAO.saveUserDO(userDO);
    }

    @Override
    public UserDO findUserById(UserDO userDO) {
        return userDAO.getUserDOByCondition(userDO);
    }

    @Override
    public List<UserDO> listUserDO() {
        return userDAO.listUserDO();
    }

    @Override
    public List<UserDO> listUserDOByCondition(UserDO userDO) {
        /*修改模糊查询的参数
         * 10，20，11------>%10%,%20%
         */
        if(userDO != null){
            if(userDO.getUserId() != null && !userDO.getUserId().equals("")){
                userDO.setUserId("%" + userDO.getUserId() + "%");
            }
        }
        return userDAO.listUserDOByCondition(userDO);
    }

    @Override
    public Long countUserDOByCondition(UserDO userDO) {
        /*修改模糊查询的参数
         * 10，20，11------>%10%,%20%
         */
        if(userDO != null){
            if(userDO.getUserId() != null && !userDO.getUserId().equals("")){
                userDO.setUserId("%" + userDO.getUserId() + "%");
            }
        }
        return userDAO.countUserDOByCondition(userDO);
    }

    @Override
    public void updateUserDO(UserDO userDO , MultipartFile file) {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String format = df.format(new Date());
        format = format + new Random().nextLong();
        String url = FileServerCommon.FILE_SERVER_HOST + format + ".jpg";//要将图片保存到网络上的完整地址
        //创建Jersey框架的客户端
        Client client = new Client();
        WebResource resource = client.resource(url);
        try {
            byte[] buf;
            buf = file.getBytes();
            resource.put(String.class,buf);//通过put方式向网络url发送资源
        } catch (IOException e) {
            e.printStackTrace();
        }
        userDO.setUserPhotoPath(url);
        System.out.println(userDO);
        userDAO.updateUserDO(userDO);
    }

    @Override
    public void deleteUserDO(UserDO userDO) {
        userDAO.deleteUserDO(userDO);
    }
}
